#include <arpa/inet.h>
#include <sys/socket.h>
#include <unistd.h>
#include <netinet/in.h>
#include <stdio.h>
#include <string.h>
#include <strings.h>
#include <stdlib.h>

#define PORT 34444

int main(int argc, const char* argv[])
{
  int ref_socket;
  struct sockaddr_in server_s,socket_com;
  const char* password="test";
  char* command=(char*)malloc(sizeof(char)*1024);
  int cpt=0,r_send;

  if(argc <= 1)
    return -1;

  for(cpt=0 ; cpt < 4 ; ++cpt)
    command[cpt]=(char)0xFF;

  command=strcat(command,"rcon ");
  command=strcat(command,password);

  for(cpt=1 ; cpt < argc ; ++cpt)
    {
      command=strcat(command," ");
      if(cpt == 1)
	command=strcat(command,"\"");
      command=strcat(command,argv[cpt]);
    }

  command=strcat(command,"\"");
  printf("%s\n",command);

  ref_socket=socket(AF_INET,SOCK_DGRAM,0);

  bzero(&server_s,sizeof(server_s));
  server_s.sin_family=AF_INET;
  server_s.sin_addr.s_addr=inet_addr("127.0.0.1");
  server_s.sin_port=htons(PORT);

  bzero(&socket_com,sizeof(socket_com));
  socket_com.sin_family=AF_INET;
  socket_com.sin_addr.s_addr=inet_addr("127.0.0.1");
  socket_com.sin_port=htons(44444);
  bind(ref_socket,(struct sockaddr*)&socket_com,sizeof(socket_com));

  r_send=sendto(ref_socket,command,strlen(command),0,(struct sockaddr*)&server_s,sizeof(server_s));

  close(ref_socket);
  free(command);
  command=NULL;

  if(r_send == -1)
    return -2;

  return 0;
}
